<template>
	<view>
		<u-popup :safeAreaInsetBottom="true" :safeAreaInsetTop="true" :mode="popupData.mode" :show="show"
			:round="popupData.round" :overlay="popupData.overlay" :borderRadius="popupData.borderRadius"
			:closeable="popupData.closeable" :closeOnClickOverlay="popupData.closeOnClickOverlay" @close="close"
			@open="open" customStyle="background-image: linear-gradient(to top, #fff1eb 0%, #ace0f9 100%);">
			<view>
				<view>

					<u-row justify="center" align="center">
						<span>手动模式</span>
					</u-row>

				</view>
				<view>
					<u-row style="height: 50vh;">
						<u-col span="2">
							<u-row justify="center" align="center">
								<span>前</span>
							</u-row>
							<u-row justify="center" align="center">
								<view class="slider-wraper">
									<ppSlider :vertical="true" :show-value="true" :value="manualData.convertLeftRightPower" :max="100"
										:block-size="20" :lineSize="15" :disabled="false" @change="beforeChanging" />
								</view>
							</u-row>
						</u-col>
						<u-col span="8">
							<u-row style="height: 10vh;" align="bottom">
								<u-subsection bgColor="transparent"  mode="button" :list="list" :current="curNow" @change="sectionChange"></u-subsection>
							</u-row>
							<u-row style="height: 40vh;">
								<view class="menu-view">
									<view class="content-view">
										<view class="one-view-view">
											<button class="one-view" @click="clickLongPress('right_top')" @longpress="longPress('right_top')" @touchend="touchend('right_top')"
												:style="{transform: 'rotate(0deg) skewY(-45deg)'}"></button>
											<button class="one-view" @click="clickLongPress('right_down')" @longpress="longPress('right_down')" @touchend="touchend('right_down')"
												:style="{transform: 'rotate(90deg) skewY(-45deg)'}"></button>
											<button class="one-view" @click="clickLongPress('down')" @longpress="longPress('down')" @touchend="touchend('down')"
												:style="{transform: 'rotate(135deg) skewY(-45deg)'}"></button>
											<button class="one-view" @click="clickLongPress('left_down')" @longpress="longPress('left_down')" @touchend="touchend('left_down')"
												:style="{transform: 'rotate(220deg) skewY(-45deg)'}"></button>
											<button class="one-view" @click="clickLongPress('left_top')" @longpress="longPress('left_top')" @touchend="touchend('left_top')"
												:style="{transform: 'rotate(270deg) skewY(-45deg)'}"></button>
											<button class="one-view" @click="clickLongPress('top')" @longpress="longPress('top')" @touchend="touchend('top')"
												:style="{transform: 'rotate(315deg) skewY(-45deg)'}"></button>
										</view>
										<!-- 方向 -->
										<view class="two-view">
											<!--右后-->
											<view class="img-view" style="transform: rotate(45.5deg);">
												<view class="img-view-child">
													<u-icon name="arrow-up-fill" :color="controlColor.right_down" :size="28"
														class="calss-img1"></u-icon>
												</view>
											</view>
											<!--下-->
											<view class="img-view" style="transform: rotate(110.5deg);">
												<view class="img-view-child">
													<u-icon name="arrow-up-fill" :color="controlColor.down" size="28"
														class="calss-img1"></u-icon>
												</view>
											</view>
											<!--后左-->
											<view class="img-view" style="transform: rotate(170.5deg);">
												<view class="img-view-child">
													<u-icon name="arrow-up-fill" :color="controlColor.left_down" size="28"
														class="calss-img1"></u-icon>
												</view>
											</view>
											<!--前左-->
											<view class="img-view" style="transform: rotate(237.5deg);">
												<view class="img-view-child">
													<u-icon name="arrow-up-fill" :color="controlColor.left_top" size="28"
														class="calss-img1"></u-icon>
												</view>
											</view>
											<!--前-->
											<view class="img-view" style="transform: rotate(290.5deg);">
												<view class="img-view-child">
													<u-icon name="arrow-up-fill" :color="controlColor.top" size="28"
														class="calss-img1"></u-icon>
												</view>
											</view>
											<!--右前-->
											<view class="img-view" style="transform: rotate(347.5deg);">
												<view class="img-view-child">
													<u-icon name="arrow-up-fill" :color="controlColor.right_top" size="28"
														class="calss-img1"></u-icon>
												</view>
											</view>
										</view>
										<!-- 开始/暂停 -->
										<view class="start-view">
											<!-- <image v-if="videoType" src="../static/imgs/kaishi.png"></image>
										<image v-else src="../static/imgs/zanting.png"></image> -->
										</view>
									</view>
								</view>
							</u-row>
							
							
						</u-col>
						<u-col span="2">
							<u-row justify="center" align="center">
								<span>后</span>
							</u-row>
							<u-row justify="center" align="center">
								<view class="slider-wraper">
									<ppSlider :vertical="true" :show-value="true" :value="manualData.convertLeftAfterPower" :max="100"
										:block-size="20" :lineSize="15" :disabled="false" @change="afterChanging" />
								</view>
							</u-row>
						</u-col>
					</u-row>
				</view>
			</view>
		</u-popup>
	</view>
</template>


<script>
	import ppSlider from "@/uni_modules/pp-slider/components/pp-slider/pp-slider.vue"
	export default {
		components: {
			ppSlider
		},
		props: {
			manualData: {
				type: Object, // 根据需要调整类型
				default: undefined // 默认值
			}
		},
		data() {
			return {
				show: false,
				popupData: {
					overlay: true,
					mode: 'bottom',
					borderRadius: '10',
					closeable: true,
					closeOnClickOverlay: true,
					round: 10
				},
				list: ['进气', '出气'],
				curNow: 0,
				timer: null, // 定时器ID
				sendCount: 0, // 发送计数器
				interval: 100, // 发送间隔(毫秒)
				delay: 50, // 长按判定延迟(毫秒)
				isPressing: false, // 按压状态
				manualTimer: null,
				revertData: null,
				controlColor:{
					'top':'#ffffff',
					'right_top':'#ffffff',
					'right_down':'#ffffff',
					'down':'#ffffff',
					'left_down':'#ffffff',
					'left_top':'#ffffff',
				}
			}
		},
		methods: {
			open() {
				this.show = true
			},
			close() {
				this.show = false
			},
			sectionChange(index) {
				this.curNow = index;
			},
			beforeChanging(value) {
				let rawStr = '00000000';
				this.$emit("pSendData", rawStr, value, this.manualData.convertLeftAfterPower);
			},
			afterChanging(val) {
				let rawStr = '00000000';
				this.$emit("pSendData", rawStr, this.manualData.convertLeftRightPower, value);
			},
			clickLongPress(direction){
				let that = this;
				that.controlColor[direction]='#2878FF';
				let bitData='';
				if(this.curNow==0){
					//说明是进气
					if(direction=='right_top'){
						this.sendParentData(2)
					}else if(direction=='left_top'){
						this.sendParentData(0);
					}else if(direction=='right_down'){
						this.sendParentData(6);
					}else if(direction=='left_down'){
						this.sendParentData(4);
					}else if(direction=='top'){
						this.sendMiddleParentData(0,2);
					}else if(direction=='down'){
						this.sendMiddleParentData(4,6);
					}
				}else if(this.curNow==1){
					//说明是出气
					if(direction=='right_top'){
						this.sendParentData(3)
					}else if(direction=='left_top'){
						this.sendParentData(1);
					}else if(direction=='right_down'){
						this.sendParentData(7);
					}else if(direction=='left_down'){
						this.sendParentData(5);
					}else if(direction=='top'){
						this.sendMiddleParentData(1,3);
					}else if(direction=='down'){
						this.sendMiddleParentData(5,7);
					}
				}
			},
			longPress(direction) {
				let that = this;
				that.isPressing = true;
				that.controlColor[direction]='#2878FF';
				let bitData='';
				if(this.curNow==0){
					//说明是进气
					if(direction=='right_top'){
						this.checkPress(2)
					}else if(direction=='left_top'){
						this.checkPress(0);
					}else if(direction=='right_down'){
						this.checkPress(6);
					}else if(direction=='left_down'){
						this.checkPress(4);
					}else if(direction=='top'){
						this.checkMiddlePress(0,2);
					}else if(direction=='down'){
						this.checkMiddlePress(4,6);
					}
				}else if(this.curNow==1){
					//说明是出气
					if(direction=='right_top'){
						this.checkPress(3)
					}else if(direction=='left_top'){
						this.checkPress(1);
					}else if(direction=='right_down'){
						this.checkPress(7);
					}else if(direction=='left_down'){
						this.checkPress(5);
					}else if(direction=='top'){
						this.checkMiddlePress(1,3);
					}else if(direction=='down'){
						this.checkMiddlePress(5,7);
					}
				}
				
			},
			checkPress(bitData){
				let that=this;
				that.sendParentData(bitData)
				if (that.isPressing) {
					that.timer = setInterval(() => {
						that.sendParentData(bitData)
					}, 100); // 200毫秒触发一次
				}
			},
			touchend(direction){
				setTimeout(() => {
					let that = this;
					this.isPressing = false
					clearInterval(this.timer); // 清除计时器
					this.timer = null;
					let rawStr = '00000000';
					setTimeout(function() {
						that.$emit("pSendData", rawStr, that.manualData.convertLeftRightPower, that
							.manualData
							.convertLeftAfterPower);
							that.controlColor[direction]='#ffffff';
					}, 100);
				}, 200);
				
			},
			sendParentData(bitData) {
				//bitData是下标，组装完成后再反转字符串，得到正确的二进制字符串
				let rawData = '00000000'
				let rawArr = rawData.split('');
				rawArr[bitData] = 1;
				let rawStr = rawArr.join('');
				let newRawStr = rawStr.split('').reverse().join('');
				this.isSend = true;
				this.$emit("pSendData", newRawStr, this.manualData.convertLeftRightPower, this.manualData
					.convertLeftAfterPower);
			},
			checkMiddlePress(bit1, bit2) {
				let that = this;
				that.isPressing = true;
				that.sendMiddleParentData(bit1, bit2)
				// 设置延迟后启动定时器（避免立即连发）
				if (that.isPressing) {
					that.timer = setInterval(() => {
						that.sendMiddleParentData(bit1, bit2)
					}, 100); // 200毫秒触发一次
				}
			},
			sendMiddleParentData(bit1, bit2) {
				//bitData是下标，组装完成后再反转字符串，得到正确的二进制字符串
				let rawData = '00000000'
				let rawArr = rawData.split('');
				rawArr[bit1] = 1;
				rawArr[bit2] = 1;
				let rawStr = rawArr.join('');
				let newRawStr = rawStr.split('').reverse().join('');
				this.isSend = true;
				this.$emit("pSendData", newRawStr, this.manualData.convertLeftRightPower, this.manualData
					.convertLeftAfterPower);
			},
		}
	}
</script>

<style scoped>
	.manual_class {
		height: 100%;

	}

	.text-calss {
		width: 100%;
		padding: 50rpx;
		color: $uni-color-warning;
	}

	button {
		padding-left: 0;
		padding-right: 0;
		background-color: transparent;
		border: 0;
		border-radius: 0;
		overflow: visible;
	}

	button::after {
		border: none;
	}

	.menu-view {
		width: 100%;
	}

	.content-view {
		position: relative;
		width: 400rpx;
		height: 400rpx;
		border-radius: 50%;
		background: #c1dcfe;
		margin: auto;
		transform: rotate(22.5deg);
		overflow: hidden;
	}

	.two-view {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		z-index: 1;
		overflow: hidden;
	}

	.one-view-view {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		overflow: hidden;
		z-index: 2;
	}

	.one-view {
		width: 400rpx;
		height: 400rpx;
		position: absolute;
		left: 50%;
		top: -50%;
		transform-origin: 0% 100%;
	}

	.img-view {
		width: 200rpx;
		height: 200rpx;
		position: absolute;
		left: 50%;
		top: 0;
		transform-origin: 0% 100%;
		overflow: hidden;
	}

	.img-view .img-view-child {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		transform: rotate(45deg) translate(0px, -11%);
	}

	.img-view .img-view-child .calss-img1 {
		width: 35rpx;
		height: 35rpx;
	}

	.img-view .img-view-child .calss-img2 {
		width: 25rpx;
		height: 25rpx;
	}

	.start-view {
		width: 220rpx;
		height: 220rpx;
		border-radius: 50%;
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%) rotate(-22.5deg);
		z-index: 3;
		display: flex;
		align-items: center;
		justify-content: center;
		background-image: linear-gradient(to top, #fff1eb 0%, #ace0f9 100%);
	}

	.content-view .start-view image {
		width: 100rpx;
		height: 100rpx;
	}

	.slider-wraper {

		height: 40vh;
	}
</style>